import json

from flask import Flask,jsonify

from creatcompwordkeysThreads import creatseekCompwordkeyThreads
from creatmidwordkeysThreads import creatseekMidkeysThreads
from dboperation import *
#这个是用来解决序列化问题的
#from JsonEncoder__没用只是不想删了 import MyEncoder
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


@app.route('/<string:seed_key>/compkeys')
def find_compkeys(seed_key):  # put application's code here
    if record_is_existed(seed_key):
        compwordkeys= find_compwordkeys(seed_key)
    else:
        #这个是因为传入的列表，要进行转换
        list = []
        list.append(seed_key)
        #这个是用来存储种子关键字的出现次数的，是为了后续的竞争度计算的，所以存起来
        #list
        num_seedkeys=creatseekMidkeysThreads(list,True)
        creatseekCompwordkeyThreads(list,num_seedkeys, 10, True)
        compwordkeys= find_compwordkeys(seed_key)
    return compwordkeys




"""
@app.route('/user')
def user():  # put application's code here
    #这个返回值是一个游标，游标不能Json序列化，所以必须转换为list类型。list(user)
    users = mongo.db.user.find()
    print(type(users))
    print(users)
    for user in users:
        print(type(user))
        print(user)
        
    #cls采用自己新的序列化方式（新加一个ObjectId判断），原来Object是不可以序列化的，这个将其转化为str类型
    #return json.dumps(newusers,cls=MyEncoder,ensure_ascii=False)
    return newusers
"""
if __name__ == '__main__':
    from gevent import pywsgi
    server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
    server.serve_forever()
    app.run()
